home *** CD-ROM | disk | FTP | other *** search
-
- // QUANTIFIERS IN ASSERTIONS FOR C++ - (c) HMMueller 1993
-
- // Author: Harald M. Mueller
- // Siemens AG Oesterreich
- // Hainburgerstr. 33
- // A-1030 Wien / Austria
- // email: mueller@garwein.hai.siemens.co.at
-
- // All the following code was written outside my work at Siemens AG
- // Oesterreich. Siemens has nothing to do with it.
-
- // Remark: This file is not at all commented. This is not good programming
- // practice, but as the following code is quite intricate, I decided to
- // put the information about its inner workings - and also about its use
- // - in a separate document, called "Powerful Assertions for C++ Without
- // Language Extensions".
-
- #ifndef _ASSERT_H_
- #define _ASSERT_H_
-
- #include <stream.h>
-
- static int asstInvert, asstResult, asstEval, asstShortCut;
-
- static struct AssertCt {
- // empty
- } asstCt;
-
- inline AssertCt& anAssertCt() {
- asstInvert = 0;
- asstEval = 1;
- return asstCt;
- }
-
- inline int asstDoEval(int& asstShortCut) {
- int eval = asstEval;
- asstShortCut = ! asstEval && asstResult;
- asstEval = 0;
- return eval;
- }
-
- inline const AssertCt& operator!(const AssertCt& a)
- {
- asstInvert = !asstInvert;
- return a;
- }
-
- inline int operator&&(int left, const AssertCt&)
- {
- asstEval = left;
- return left;
- }
-
- inline int operator||(int left, const AssertCt&)
- {
- asstEval = !left;
- return left;
- }
-
- static ostream& assertout(ostream& os, unsigned line, char* file)
- {
- return os << "Assertion in line " << line << ", file "
- << file << " failed for ";
- }
-
- // ----------------------------------------------------------------------
-
- #define asstLoopTest(asstFor,asstAll,asstCond) \
- anAssertCt(); \
- { int asstShortCut; \
- if (asstDoEval(asstShortCut)) { \
- int asstInvert = ::asstInvert; \
- asstResult = asstAll; \
- for asstFor { \
- asstResult = 0 || asstCond; \
- if (asstResult != asstAll) break; \
- } \
- if (asstInvert) asstResult = !asstResult; \
- } \
- ::asstShortCut = asstShortCut; \
- } \
- asstResult = ::asstShortCut ? asstResult : asstResult
-
- #define assertp(asstCond,asstOutput) \
- asstResult = 0 || asstCond, asstResult || \
- (assertout(ASSERTSTREAM,__LINE__,__FILE__) << asstOutput\
- << endl, ASSERTACTION, 0)
-
- #define assert(asstCond) assertp(asstCond,"condition \`" #asstCond "\'")
-
- #define forall(asstFor,asstCond) asstLoopTest(asstFor,1,asstCond)
- #define exists(asstFor,asstCond) asstLoopTest(asstFor,0,asstCond)
-
- #define old(asstname) old_##asstname
- #define saveold(assttype,asstname) assttype old_##asstname = asstname
-
- #define ASSERTSTREAM cerr
- #define ASSERTACTION exit(125)
-
- #endif
-